﻿@model ZDSoft.LMS.Domain.User

@{
    ViewBag.Title = "AssignRole";
    Layout = "~/Views/Shared/_Layout.cshtml";
    IList<ZDSoft.LMS.Domain.Role> roles = ViewBag.Roles as IList<ZDSoft.LMS.Domain.Role>;
    
    <link href="@Url.Content("~/Content/Style/ViewPage.css")" rel="stylesheet" type="text/css" />
}
<script language="javascript" type="text/javascript">
    //全选与反选
    function CheckAll() {
        // $("#chkAll").attr("checked") == true ? $("#tbRoles").find("input").attr("checked", true) : $("#tbRoles").find("input").attr("checked", false);

        var selectedIds = ",";
        if ($("#chkAll").attr("checked") == true) {
            $("#tbRoles").find("input").each(function () {
                //this.checked = "checked";
                $(this).attr("checked", true);
            });
            selectedIds = GetAllIds();
        }
        else {
            $("#tbRoles").find("input").each(function () {
                this.checked = false;
            });
        }
        $("#hdSelectedIds").val(selectedIds);
    }

    //获取所有的角色ID
    function GetAllIds() {
        var ids = ",";
        $("#tbRoles").find("input").each(function () {
            if (this.id != "chkAll") {
                ids += "," + this.id + ",";
            }
        });
        return ids;
    }

    //选择角色ID
    function SetSelectedId(ctl, id) {
        var val = "";
        if (ctl.checked) {
            val = $("#hdSelectedIds").val();
            if (val.indexOf("," + id + ",") < 0) {
                val += "," + id + ",";
            }
        }
        else {
            val = $("#hdSelectedIds").val();
            if (val.indexOf("," + id + ",") > 0) {
                val = val.replace("," + id + ",", "");
            }
        }
        $("#hdSelectedIds").val(val);
    }

    //提交之前选择角色
    function CheckSelectedRoles() {
        var val = $("#hdSelectedIds").val();
        if (val == "," || val == "") {
            if (confirm("不打算配置角色吗?")) {
                return true;
            }
            return false;
        }
    }
</script>
<h2>AssignRole</h2>

<fieldset>
    <legend>User</legend>

    <div class="display-label">UserName</div>
    <div class="display-field">@Model.UserName</div>

    <div class="display-label">Account</div>
    <div class="display-field">@Model.Account</div>
</fieldset>
<p>角色对应的功能：</p>
@using (Html.BeginForm())
{
    var roleIdList = ",";
    <!-- 隐藏的用户ID -->
    @Html.Hidden("userId",Model.ID)
<fieldset>
        <legend>配置角色</legend>
        <table cellpadding="0" cellspacing="0" class="dataGrid" id="tbRoles">
            <!-- 列头开始 -->
            <tr>                
                <th>
                    @Html.CheckBox("chkAll", false, new { onclick = "CheckAll()", id = "chkAll" })全选/取消
                </th>
                <th>
                    角色名称
                </th>
            </tr>
            <!-- 列头结束 -->

            <!-- 动态生成的行开始 -->
            @foreach (var item in roles)
            {
                <tr>
                    <td>
                    @Html.CheckBoxFor(temp => item.IsChecked, new { @id = ""+@item.ID+"", @onclick = "SetSelectedId(this,'" + @item.ID + "')" })
                    @if (item.IsChecked) 
                        {
                            //将选中的角色ID保存在变量中
                            roleIdList += "," + item.ID + ",";
                        }
                    </td>
                    <td>
                        @item.RoleName
                    </td>
                </tr>
            }
            <!-- 动态生成的行结束 -->
        </table>
        <input type="text" id="hdSelectedIds" name="hdSelectedIds" value="@roleIdList" />
        <p>
            <input type="submit" value="保存" class="btn3" onclick="return CheckSelectedRoles()" />
        </p>
    </fieldset>
}
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

